package com.bdq.interaction.application.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bdq.common.config.base.StaticDomainConfig;
import com.bdq.common.entity.common.PageResult;
import com.bdq.common.feign.user.vo.UserListVO;
import com.bdq.common.utils.StaticFileUtil;
import com.bdq.interaction.application.model.dto.*;
import com.bdq.interaction.application.model.query.*;
import com.bdq.interaction.application.service.InteractionAppQueryService;
import com.bdq.interaction.application.service.MyFeignClient;
import com.bdq.interaction.domain.model.dto.ChildCommentPageDomainDTO;
import com.bdq.interaction.domain.model.dto.InteractionDataGetDomainDTO;
import com.bdq.interaction.domain.model.dto.InteractionDataListDomainDTO;
import com.bdq.interaction.domain.model.dto.ParentCommentPageDomainDTO;
import com.bdq.interaction.domain.model.dto.req.ChildCommentPageReqDTO;
import com.bdq.interaction.domain.model.dto.req.InteractionDataGetReqDTO;
import com.bdq.interaction.domain.model.dto.req.ParentCommentPageReqDTO;
import com.bdq.interaction.domain.service.InteractionDomainService;
import com.bdq.interaction.infrastructure.persistence.mysql.pojo.InteractionSingle;
import com.bdq.interaction.infrastructure.persistence.mysql.service.InteractionSingleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author malei
 * @date 2024-05-21 14:04
 */
@Service
public class InteractionAppQueryServiceImpl implements InteractionAppQueryService {

    @Resource
    private InteractionDomainService interactionDomainService;

    @Resource
    private InteractionSingleService interactionSingleService;

    @Resource
    private MyFeignClient myFeignClient;

    @Override
    public InteractionDataGetAppDTO getData(InteractionDataGetQry qry) {
        InteractionDataGetDomainDTO interactionDataGetDomainDTO = interactionDomainService.getData(Convert.convert(InteractionDataGetReqDTO.class, qry));
        return Convert.convert(InteractionDataGetAppDTO.class, interactionDataGetDomainDTO);
    }

    @Override
    public List<InteractionDataListAppDTO> listData(Long userId, List<Long> contentIds) {
        List<InteractionDataListDomainDTO> interactionDataGetDomainDTOList = interactionDomainService.listData(userId, contentIds);
        return Convert.toList(InteractionDataListAppDTO.class, interactionDataGetDomainDTOList);
    }


    @Override
    public PageResult<ParentCommentPageAppDTO> pageParentComment(ParentCommentPageQry qry) {
        PageResult<ParentCommentPageDomainDTO> page = interactionDomainService.pageParentComment(Convert.convert(ParentCommentPageReqDTO.class, qry));
        PageResult<ParentCommentPageAppDTO> result = new PageResult<>();
        result.setTotal(page.getTotal());
        result.setList(Convert.toList(ParentCommentPageAppDTO.class, page.getList()));
        for (ParentCommentPageAppDTO parentCommentPageAppDTO : result.getList()) {
            parentCommentPageAppDTO.setAuthor(qry.getUserId().equals(parentCommentPageAppDTO.getCommentUserId()));
        }
        return result;
    }

    @Override
    public PageResult<ChildCommentPageAppDTO> pageChildComment(ChildCommentPageQry qry) {
        PageResult<ChildCommentPageDomainDTO> page = interactionDomainService.pageChildComment(Convert.convert(ChildCommentPageReqDTO.class, qry));
        PageResult<ChildCommentPageAppDTO> result = new PageResult<>();
        result.setTotal(page.getTotal());
        result.setList(Convert.toList(ChildCommentPageAppDTO.class, page.getList()));
        for (ChildCommentPageAppDTO childCommentPageAppDTO : result.getList()) {
            childCommentPageAppDTO.setAuthor(qry.getUserId().equals(childCommentPageAppDTO.getCommentUserId()));
        }
        return result;
    }

    @Override
    public PageResult<SignUpUserPageAppDTO> pageSingUpUser(SignUpUserPageQry qry) {
        // 1 分页查报名用户
        IPage<InteractionSingle> page = interactionSingleService.pageSignUp(qry.getContentId(), qry.getPageNo(), qry.getPageSize());
        PageResult<SignUpUserPageAppDTO> result = new PageResult<>();
        result.setTotal(page.getTotal());
        if (CollUtil.isEmpty(page.getRecords())) {
            result.setList(Collections.emptyList());
            return result;
        }

        // 2 报名用户信息
        List<Long> userIds = page.getRecords().stream().map(InteractionSingle::getUserId).collect(Collectors.toList());
        List<UserListVO> userListVOS = myFeignClient.listUser(userIds);
        for (UserListVO userListVO : userListVOS) {
            userListVO.setAvatar(StaticFileUtil.domainConcat(userListVO.getAvatar()));
        }
        result.setList(Convert.toList(SignUpUserPageAppDTO.class, userListVOS));
        return result;
    }

    @Override
    public PageResult<Long> pageUserInteraction(UserInteractionPageQry qry) {
        IPage<InteractionSingle> page = interactionSingleService.page(qry.getUserId(), qry.getType(), qry.getPageNo(), qry.getPageSize());
        List<Long> contentIds = page.getRecords().stream().map(InteractionSingle::getContentId).collect(Collectors.toList());
        PageResult<Long> result = new PageResult<>();
        result.setTotal(page.getTotal());
        result.setList(contentIds);
        return result;
    }
}
